import { IChartsBaseData } from 'src/interface';

interface IChartAreaOptions {
  value: (string | number)[];
  lineColor?: string;
  areaColor?: string;
}

export const extractData = (
  data: IChartsBaseData[],
): {
  legend: string[];
  axis: string[];
  options: Record<string, IChartAreaOptions>;
  color: string[];
} => {
  const legendList = new Set<string>();
  const axisList = new Set<string>();
  const options = {} as Record<string, IChartAreaOptions>;
  const baseColor = new Set<string>();
  data.forEach((item) => {
    legendList.add(item.legendName);
    axisList.add(item.axisName);
    options[item.legendName]?.value
      ? options[item.legendName].value.push(item.value)
      : (options[item.legendName] = { value: [item.value] });
    !options[item.legendName]?.lineColor && (options[item.legendName].lineColor = item.lineColor);
    !options[item.legendName]?.areaColor && (options[item.legendName].areaColor = item.areaColor);
    baseColor.add(item.color || item.lineColor || item.areaColor || '');
  });
  return {
    legend: Array.from(legendList) ?? [],
    axis: Array.from(axisList) ?? [],
    options,
    color: Array.from(baseColor) ?? [],
  };
};
